package cn.ultrarisk.scadmin.manager.biz.monitor.service;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.dao.SolrNodeMonitorDAO;
import cn.ultrarisk.scadmin.manager.biz.monitor.entity.SolrNodeMonitor;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import cn.ultrarisk.scadmin.manager.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * solr节点监控数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SolrNodeMonitorService {
    @Autowired
    private SolrNodeMonitorDAO solrNodeMonitorDAO;

    public SolrNodeMonitor createBySolrInfo(Integer nodeId, JsonUtil.MapWrapper jvmMap, JsonUtil.MapWrapper systemMap, Long createTime) {
        SolrNodeMonitor monitor = new SolrNodeMonitor(nodeId, SolrNodeMonitor.STATUS_ALIVE);
        monitor.setJvmTotalMemory(jvmMap.getLongByPath("memory.raw.total"));
        monitor.setJvmFreeMemory(jvmMap.getLongByPath("memory.raw.free"));
        monitor.setJvmMaxMemory(jvmMap.getLongByPath("memory.raw.max"));
        monitor.setJvmUsedMemory(jvmMap.getLongByPath("memory.raw.used"));
        monitor.setJvmUsedMemoryPercent(jvmMap.getDoubleByPath("memory.raw.used%"));
        monitor.setJvmUptime(jvmMap.getLongByPath("jmx.upTimeMS"));

        monitor.setSystemCommittedVirtualMemory(systemMap.getLongByPath("committedVirtualMemorySize"));
        monitor.setSystemFreePhysicalMemory(systemMap.getLongByPath("freePhysicalMemorySize"));
        monitor.setSystemTotalPhysicalMemory(systemMap.getLongByPath("totalPhysicalMemorySize"));
        monitor.setSystemFreeSwapSpace(systemMap.getLongByPath("freeSwapSpaceSize"));
        monitor.setSystemTotalSwapSpace(systemMap.getLongByPath("totalSwapSpaceSize"));
        monitor.setSystemOpenFileDescriptorCount(systemMap.getIntByPath("openFileDescriptorCount"));
        monitor.setSystemMaxFileDescriptorCount(systemMap.getIntByPath("maxFileDescriptorCount"));

        try {
            List<String> systemTops = CollectionUtil.split(systemMap.getStringByPath("uptime"), ",");
            monitor.setSystemLoadAverage1min(Double.parseDouble(CollectionUtil.split(systemTops.get(systemTops.size() - 3), ":").get(1)));
            monitor.setSystemLoadAverage5min(Double.parseDouble(systemTops.get(systemTops.size() - 2)));
            monitor.setSystemLoadAverage15min(Double.parseDouble(systemTops.get(systemTops.size() - 1)));
            monitor.setSystemUptime(systemTops.get(0));
        } catch (Exception ignored) {
        }

        monitor.setCreateTime(createTime);
        solrNodeMonitorDAO.create(monitor);
        return monitor;
    }

    /**
     * 清除过期的SolrCache监控数据
     *
     * @query expiredTime
     */
    public void clearExpiredData(long expiredTime) {
        Query query = Query.ltQuery(SolrNodeMonitor.CREATE_TIME, expiredTime);
        solrNodeMonitorDAO.delete(query);
    }
}